package com.book.service.impl;

import com.book.dao.AddressDao;
import com.book.dto.AddressDto;
import com.book.helper.AddressHelper;
import com.book.pojo.Address;
import com.book.service.AddressService;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
    @Override
    public Address getAddressById(Integer integer) {
        return addressDao.selectByPrimaryKey(integer);
    }

    @Override
    public List<AddressDto> getList(String uCode) {
        List<Address> addressListByUCode = addressDao.getAddressListByUCode(uCode);
        if (CollectionUtils.isEmpty(addressListByUCode)){
            return Lists.newArrayList();
        }
        return addressListByUCode.stream().map(AddressHelper::covertPoDto).collect(Collectors.toList());
    }

    @Override
    public Boolean updateAddress(AddressDto addressDto) {
        Address address = AddressHelper.covertDtoPo(addressDto);
        return addressDao.updateByPrimaryKey(address) == 1;
    }

    @Override
    public Integer addAddr(AddressDto addressDto) {
        return addressDao.insertSelective(AddressHelper.covertDtoPo(addressDto));
    }

    @Override
    public Boolean delete(Integer id) {
        return addressDao.deleteByPrimaryKey(id) == 1;
    }
}
